﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ChietKhauSanPhamProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ChietKhauSanPhamProviderBaseCore : EntityProviderBase<ATA.Entities.ChietKhauSanPham, ATA.Entities.ChietKhauSanPhamKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.ChietKhauSanPhamKey key)
		{
			return Delete(transactionManager, key.ChietKhauSpid);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="chietKhauSpid">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid chietKhauSpid)
		{
			return Delete(null, chietKhauSpid);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauSpid">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid chietKhauSpid);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		FK_ChietKhauSanPham_NhomSanPham Description: 
		/// </summary>
		/// <param name="nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(System.Guid nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(nhomSanPhamId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		FK_ChietKhauSanPham_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, nhomSanPhamId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		FK_ChietKhauSanPham_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId, int start, int pageLength)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, nhomSanPhamId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		fkChietKhauSanPhamNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomSanPhamId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(System.Guid nhomSanPhamId, int start, int pageLength)
		{
			int count =  -1;
			return GetByNhomSanPhamId(null, nhomSanPhamId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		fkChietKhauSanPhamNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(System.Guid nhomSanPhamId, int start, int pageLength,out int count)
		{
			return GetByNhomSanPhamId(null, nhomSanPhamId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_NhomSanPham key.
		///		FK_ChietKhauSanPham_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public abstract ATA.Entities.TList<ChietKhauSanPham> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		FK_ChietKhauSanPham_ChietKhau Description: 
		/// </summary>
		/// <param name="chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(chietKhauId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		FK_ChietKhauSanPham_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		FK_ChietKhauSanPham_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		fkChietKhauSanPhamChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chietKhauId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength)
		{
			int count =  -1;
			return GetByChietKhauId(null, chietKhauId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		fkChietKhauSanPhamChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chietKhauId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength,out int count)
		{
			return GetByChietKhauId(null, chietKhauId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_ChietKhau key.
		///		FK_ChietKhauSanPham_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public abstract ATA.Entities.TList<ChietKhauSanPham> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		FK_ChietKhauSanPham_DonViTinh Description: 
		/// </summary>
		/// <param name="donViTinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(System.Byte donViTinhId)
		{
			int count = -1;
			return GetByDonViTinhId(donViTinhId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		FK_ChietKhauSanPham_DonViTinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="donViTinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(TransactionManager transactionManager, System.Byte donViTinhId)
		{
			int count = -1;
			return GetByDonViTinhId(transactionManager, donViTinhId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		FK_ChietKhauSanPham_DonViTinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="donViTinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(TransactionManager transactionManager, System.Byte donViTinhId, int start, int pageLength)
		{
			int count = -1;
			return GetByDonViTinhId(transactionManager, donViTinhId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		fkChietKhauSanPhamDonViTinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="donViTinhId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(System.Byte donViTinhId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDonViTinhId(null, donViTinhId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		fkChietKhauSanPhamDonViTinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="donViTinhId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(System.Byte donViTinhId, int start, int pageLength,out int count)
		{
			return GetByDonViTinhId(null, donViTinhId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauSanPham_DonViTinh key.
		///		FK_ChietKhauSanPham_DonViTinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="donViTinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauSanPham objects.</returns>
		public abstract ATA.Entities.TList<ChietKhauSanPham> GetByDonViTinhId(TransactionManager transactionManager, System.Byte donViTinhId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.ChietKhauSanPham Get(TransactionManager transactionManager, ATA.Entities.ChietKhauSanPhamKey key, int start, int pageLength)
		{
			return GetByChietKhauSpid(transactionManager, key.ChietKhauSpid, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="chietKhauSpid"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(System.Guid chietKhauSpid)
		{
			int count = -1;
			return GetByChietKhauSpid(null,chietKhauSpid, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="chietKhauSpid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(System.Guid chietKhauSpid, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauSpid(null, chietKhauSpid, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauSpid"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(TransactionManager transactionManager, System.Guid chietKhauSpid)
		{
			int count = -1;
			return GetByChietKhauSpid(transactionManager, chietKhauSpid, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauSpid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(TransactionManager transactionManager, System.Guid chietKhauSpid, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauSpid(transactionManager, chietKhauSpid, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="chietKhauSpid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(System.Guid chietKhauSpid, int start, int pageLength, out int count)
		{
			return GetByChietKhauSpid(null, chietKhauSpid, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauSanPham index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauSpid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauSanPham"/> class.</returns>
		public abstract ATA.Entities.ChietKhauSanPham GetByChietKhauSpid(TransactionManager transactionManager, System.Guid chietKhauSpid, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a ATA.Entities.TList&lt;ChietKhauSanPham&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="ATA.Entities.TList&lt;ChietKhauSanPham&gt;"/></returns>
		public static ATA.Entities.TList<ChietKhauSanPham> Fill(IDataReader reader, ATA.Entities.TList<ChietKhauSanPham> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			ATA.Entities.ChietKhauSanPham c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("ChietKhauSanPham")
			.Append("|").Append((reader.IsDBNull(((int)ATA.Entities.ChietKhauSanPhamColumn.ChietKhauSpid - 1))?Guid.Empty:(System.Guid)reader[((int)ATA.Entities.ChietKhauSanPhamColumn.ChietKhauSpid - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<ChietKhauSanPham>(
			key.ToString(), // EntityTrackingKey
			"ChietKhauSanPham",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new ATA.Entities.ChietKhauSanPham();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.ChietKhauSpid = (System.Guid)reader["ChietKhauSPID"];
			c.OriginalChietKhauSpid = c.ChietKhauSpid;
			c.NhomSanPhamId = (System.Guid)reader["NhomSanPhamID"];
			c.DonViTinhId = (System.Byte)reader["DonViTinhID"];
			c.SoLuong = (System.Byte)reader["SoLuong"];
			c.MucThuong = (System.Double)reader["MucThuong"];
			c.LoaiThuong = (System.Boolean)reader["LoaiThuong"];
			c.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			c.LastUpdate = (System.DateTime)reader["LastUpdate"];
			c.UpdateBy = (System.Int32)reader["UpdateBy"];
			c.HinhThucThuong = reader.IsDBNull(reader.GetOrdinal("HinhThucThuong")) ? null : (System.String)reader["HinhThucThuong"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauSanPham"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauSanPham"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.ChietKhauSanPham entity)
		{
			if (!reader.Read()) return;
			
			entity.ChietKhauSpid = (System.Guid)reader["ChietKhauSPID"];
			entity.OriginalChietKhauSpid = (System.Guid)reader["ChietKhauSPID"];
			entity.NhomSanPhamId = (System.Guid)reader["NhomSanPhamID"];
			entity.DonViTinhId = (System.Byte)reader["DonViTinhID"];
			entity.SoLuong = (System.Byte)reader["SoLuong"];
			entity.MucThuong = (System.Double)reader["MucThuong"];
			entity.LoaiThuong = (System.Boolean)reader["LoaiThuong"];
			entity.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			entity.LastUpdate = (System.DateTime)reader["LastUpdate"];
			entity.UpdateBy = (System.Int32)reader["UpdateBy"];
			entity.HinhThucThuong = reader.IsDBNull(reader.GetOrdinal("HinhThucThuong")) ? null : (System.String)reader["HinhThucThuong"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauSanPham"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauSanPham"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.ChietKhauSanPham entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ChietKhauSpid = (System.Guid)dataRow["ChietKhauSPID"];
			entity.OriginalChietKhauSpid = (System.Guid)dataRow["ChietKhauSPID"];
			entity.NhomSanPhamId = (System.Guid)dataRow["NhomSanPhamID"];
			entity.DonViTinhId = (System.Byte)dataRow["DonViTinhID"];
			entity.SoLuong = (System.Byte)dataRow["SoLuong"];
			entity.MucThuong = (System.Double)dataRow["MucThuong"];
			entity.LoaiThuong = (System.Boolean)dataRow["LoaiThuong"];
			entity.ChietKhauId = (System.Guid)dataRow["ChietKhauID"];
			entity.LastUpdate = (System.DateTime)dataRow["LastUpdate"];
			entity.UpdateBy = (System.Int32)dataRow["UpdateBy"];
			entity.HinhThucThuong = Convert.IsDBNull(dataRow["HinhThucThuong"]) ? null : (System.String)dataRow["HinhThucThuong"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauSanPham"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauSanPham Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, ATA.Entities.ChietKhauSanPham entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region NhomSanPhamIdSource	
			if (CanDeepLoad(entity, "NhomSanPham|NhomSanPhamIdSource", deepLoadType, innerList) 
				&& entity.NhomSanPhamIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.NhomSanPhamId;
				NhomSanPham tmpEntity = EntityManager.LocateEntity<NhomSanPham>(EntityLocator.ConstructKeyFromPkItems(typeof(NhomSanPham), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.NhomSanPhamIdSource = tmpEntity;
				else
					entity.NhomSanPhamIdSource = DataRepository.NhomSanPhamProvider.GetByNhomSanPhamId(transactionManager, entity.NhomSanPhamId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhomSanPhamIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhomSanPhamIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.NhomSanPhamProvider.DeepLoad(transactionManager, entity.NhomSanPhamIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion NhomSanPhamIdSource

			#region ChietKhauIdSource	
			if (CanDeepLoad(entity, "ChietKhau|ChietKhauIdSource", deepLoadType, innerList) 
				&& entity.ChietKhauIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ChietKhauId;
				ChietKhau tmpEntity = EntityManager.LocateEntity<ChietKhau>(EntityLocator.ConstructKeyFromPkItems(typeof(ChietKhau), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ChietKhauIdSource = tmpEntity;
				else
					entity.ChietKhauIdSource = DataRepository.ChietKhauProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ChietKhauIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ChietKhauProvider.DeepLoad(transactionManager, entity.ChietKhauIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ChietKhauIdSource

			#region DonViTinhIdSource	
			if (CanDeepLoad(entity, "DonViTinh|DonViTinhIdSource", deepLoadType, innerList) 
				&& entity.DonViTinhIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.DonViTinhId;
				DonViTinh tmpEntity = EntityManager.LocateEntity<DonViTinh>(EntityLocator.ConstructKeyFromPkItems(typeof(DonViTinh), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DonViTinhIdSource = tmpEntity;
				else
					entity.DonViTinhIdSource = DataRepository.DonViTinhProvider.GetByDonViTinhId(transactionManager, entity.DonViTinhId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DonViTinhIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DonViTinhIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DonViTinhProvider.DeepLoad(transactionManager, entity.DonViTinhIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DonViTinhIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.ChietKhauSanPham object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.ChietKhauSanPham instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauSanPham Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, ATA.Entities.ChietKhauSanPham entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region NhomSanPhamIdSource
			if (CanDeepSave(entity, "NhomSanPham|NhomSanPhamIdSource", deepSaveType, innerList) 
				&& entity.NhomSanPhamIdSource != null)
			{
				DataRepository.NhomSanPhamProvider.Save(transactionManager, entity.NhomSanPhamIdSource);
				entity.NhomSanPhamId = entity.NhomSanPhamIdSource.NhomSanPhamId;
			}
			#endregion 
			
			#region ChietKhauIdSource
			if (CanDeepSave(entity, "ChietKhau|ChietKhauIdSource", deepSaveType, innerList) 
				&& entity.ChietKhauIdSource != null)
			{
				DataRepository.ChietKhauProvider.Save(transactionManager, entity.ChietKhauIdSource);
				entity.ChietKhauId = entity.ChietKhauIdSource.ChietKhauId;
			}
			#endregion 
			
			#region DonViTinhIdSource
			if (CanDeepSave(entity, "DonViTinh|DonViTinhIdSource", deepSaveType, innerList) 
				&& entity.DonViTinhIdSource != null)
			{
				DataRepository.DonViTinhProvider.Save(transactionManager, entity.DonViTinhIdSource);
				entity.DonViTinhId = entity.DonViTinhIdSource.DonViTinhId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ChietKhauSanPhamChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.ChietKhauSanPham</c>
	///</summary>
	public enum ChietKhauSanPhamChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>NhomSanPham</c> at NhomSanPhamIdSource
		///</summary>
		[ChildEntityType(typeof(NhomSanPham))]
		NhomSanPham,
			
		///<summary>
		/// Composite Property for <c>ChietKhau</c> at ChietKhauIdSource
		///</summary>
		[ChildEntityType(typeof(ChietKhau))]
		ChietKhau,
			
		///<summary>
		/// Composite Property for <c>DonViTinh</c> at DonViTinhIdSource
		///</summary>
		[ChildEntityType(typeof(DonViTinh))]
		DonViTinh,
		}
	
	#endregion ChietKhauSanPhamChildEntityTypes
	
	#region ChietKhauSanPhamFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ChietKhauSanPhamColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauSanPham"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauSanPhamFilterBuilder : SqlFilterBuilder<ChietKhauSanPhamColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamFilterBuilder class.
		/// </summary>
		public ChietKhauSanPhamFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauSanPhamFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauSanPhamFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauSanPhamFilterBuilder
	
	#region ChietKhauSanPhamParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ChietKhauSanPhamColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauSanPham"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauSanPhamParameterBuilder : ParameterizedSqlFilterBuilder<ChietKhauSanPhamColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamParameterBuilder class.
		/// </summary>
		public ChietKhauSanPhamParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauSanPhamParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauSanPhamParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauSanPhamParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauSanPhamParameterBuilder
} // end namespace
